// 入口文件
import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter);
import router from "./router.js";

// 导入时间插件
import moment from "moment";
// 定义全局过滤器
Vue.filter('dataFormat',function(dataStr,pattern = "YYYY-MM-DD hh:mm:ss"){
  return moment(dataStr).format(pattern)
})

// 注册 vuex
import Vuex from 'vuex';
Vue.use(Vuex)

var car = JSON.parse(localStorage.getItem('car') || '[]')

var store = new Vuex.Store({
  state:{//this.$store.state.***
    car:car
  },
  mutations:{//this.$store.commit('方法名','按需传递唯一的参数')
    addToCar(state,goodsinfo){
      var flag =false
      state.car.some(item =>{
        if(item.id==goodsinfo.id){
          item.count +=parseInt(goodsinfo.count)
          flag=true
          return true
        }
      })
      if(!flag){
        state.car.push(goodsinfo)
      }
      localStorage.setItem('car',JSON.stringify(state.car))
    }
  },
  getters:{//this.$store.getters.***
    getAllCount(state){
      var c=0;
      state.car.forEach(item=>{
        c+=item.count
      })
      return c
    },
    getGoodsCount(state){
      var o ={}
      state.car.forEach(item=>{
        o[item.id]=item.count
      })
      return o
    }
  }
})

import VueResource from "vue-resource";
Vue.use(VueResource);
// Vue.http.option.root = 'http://vue.studyit.io';
// 全局设置post时候表单数据格式组织形式
Vue.http.options.emulateJSON = true;

// 导入MUI的样式
import "./lib/mui/css/mui.min.css";
import "./lib/mui/css/icons-extra.css";

// 按需导入mint-ui中的组件
// import { Header, Swipe, SwipeItem,Button,Lazyload} from "mint-ui";
// Vue.component(Header.name, Header);
// Vue.component(Swipe.name, Swipe);
// Vue.component(SwipeItem.name, SwipeItem);
// Vue.component(Button.name, Button);
// Vue.use(Lazyload);
import MintUI from 'mint-ui'
Vue.use(MintUI);
import 'mint-ui/lib/style.css';

// 安装图片预览插件
import VuePreview from 'vue-preview';
Vue.use(VuePreview);

// 导入 App 根组件
import app from "./App.vue";

var vm = new Vue({
  el: "#app",
  render: (c) => c(app),
  router,
  store,
});
